<?php

namespace common\repositories\myhtsq;

use common\entities\myhtsq\MyscrmRoleActionEntity;
use Yii;
use common\error\ErrorCode;
use common\entities\myhtsq\RoleActionPackageEntity;
use common\repositories\BaseRepository;

class RoleActionPackageRepository extends BaseRepository {

    /**
     * 构造方法
     * @param unknown $config (optional)
     */
    public function __construct($config = []) {
        parent::__construct($config);
    }

    

    /**
     * 根据角色id,获取角色授权包
     * @param int $roleId 角色id
     * @return array
     */
    public static function getAllRoleActionPackageByRoleId($roleId) {
        return RoleActionPackageEntity::find()->where(['role_id' => $roleId])->all();
    }

    /**
     * @param $role_id
     * @param $pockage_data
     * @return int
     * @throws \yii\db\Exception
     */
    public function saveRoleAuth($role_id, $pockage_data)
    {
        $pockage_data = json_decode($pockage_data, true);
        MyscrmRoleActionEntity::deleteAll(['role_id' => $role_id]);
        $insertSql = '';
        foreach ($pockage_data as  $k => $v){
            $insertSql .= "INSERT INTO myscrm_role_action (role_id,action_id) VALUES($role_id, $v);";
        }
        $connection = MyscrmRoleActionEntity::getDb();
        return $connection->createCommand($insertSql)->execute();
    }
}
